var roleE9S59_HC = {
    run: function(creep) {

        //creep.say(creep.memory.building);

        if(creep.store.getUsedCapacity()==0){
            creep.memory.building = true;
        }
        if(creep.store.getFreeCapacity()==0){
            creep.memory.building = false;
        }


        if(creep.memory.building == true){
                //creep.say(321321);
                var sources = creep.room.find(FIND_SOURCES);
                if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
                    creep.moveTo(sources[0],  {reusePath:10});
                }
        }
        else{     
                  
                        var targetsstorage = creep.room.find(FIND_STRUCTURES, {
                            filter: (structure) => {
                                return (structure.structureType == STRUCTURE_STORAGE) && 
                                structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0;
                            }
                        });
                        if(targetsstorage.length > 0) {
                            if(creep.transfer(targetsstorage[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
                                creep.moveTo(targetsstorage[0], {reusePath:10});
                                //creep.say('搬去');
                            }
                        } 
                        if(0){
                            //creep.say('运去3');
            var targets = creep.room.find(FIND_STRUCTURES, {
                filter: (structure) => {
                    return ((structure.structureType == STRUCTURE_EXTENSION ||
                            structure.structureType == STRUCTURE_SPAWN) && 
                            structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0);
                }
            });
            if(targets.length > 0) {
                
                //creep.say('运2去');
                if(targets.length>0){
                    if(creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
                        creep.moveTo(targets[0]);
                        //creep.say('运去');
                    }
                }
                
            }
                        }
        }
	}
};

module.exports = roleE9S59_HC;